#pragma once
#include "../db_common.h"
#include "catalog/catalog_manager.h"
#include "logger/logger.h"
#include "interface.h"
#include "../bplus_tree/bplus_tree.h"
#include <vector>
#include <unordered_map>
#include <string>
#include <optional>

class Executor
{
public:
    Executor(CatalogManager &catalog)
        : catalog_(catalog), logger_(Logger::GetInstance()) {}

    // 执行入口：接收执行计划，返回结果
    QueryResult executePlan(const ExecutionPlanNode &plan);

private:
    // 算子执行函数（按类型分发）
    QueryResult executeOp(const ExecutionPlanNode &plan);
    // 具体算子实现
    std::vector<std::vector<Value>> executeSeqScan(const std::string &table_name);                       // 全表扫描
    std::vector<std::vector<Value>> executeFilter(const Condition &cond, const std::string &table_name); // 过滤
    std::vector<std::vector<Value>> executeProject(std::vector<std::vector<Value>> &input, const std::vector<std::string> &cols, const std::string &table_name,
                                                   const std::optional<uint16_t> &offset, const std::optional<uint16_t> &limit,
                                                   const std::vector<OrderBy> &orders);                                        // 投影
    bool executeCreateTable(const std::string &table_name, const std::vector<ColumnSchema> &cols, const std::string &comment); // 创建表
    bool executeInsert(const std::string &table_name, const std::unordered_map<std::string, Value> &values);                   // 插入数据
    bool executeDropTable(const std::string &table_name);
    bool executeCreateIndex(const std::string &table_name, const std::string &index_name, const std::vector<std::string> &columns, bool unique); // 创建索引
    bool executeDropIndex(const std::string &table_name, const std::string &index_name);                                                         // 删除索引
    size_t executeDelete(const std::string &table_name, const Condition &condition);
    size_t executeUpdate(const std::string &table_name, const Condition &condition, const std::unordered_map<std::string, Value> &values);
    bool executeAlterTable(const std::string &table_name, std::optional<std::string> rename_table_name, std::optional<std::string> modified_comment,
                           std::optional<std::unordered_map<std::string, ModifiedColumn>> modified_cols, std::optional<std::unordered_map<std::string, std::string>> modified_indexes, std::optional<std::vector<std::string>> drop_cols, std::optional<std::vector<ColumnSchema>> add_cols);
    std::unordered_map<std::string, std::vector<ColumnSchema>> executeShowTables();
    std::vector<IndexSchema> executeShowIndexes(const std::string &table_name);

private:
    CatalogManager &catalog_; // 系统目录（依赖注入）
    Logger &logger_;          // 日志

    // 辅助函数：检查插入值与列类型匹配
    std::pair<bool, std::string> checkInsertType(const std::unordered_map<std::string, Value> &values, const std::string &table_name, std::vector<Value> &row);
    // 递归函数，用于评估一行数据是否满足一个复杂的条件树
    bool evaluateCondition(const Row &row, const TableSchema &schema, const Condition &cond);

    const Condition *findIndexableCondition(const Condition &cond, const TableSchema &schema);

    bool validateColumn(const ColumnSchema &col);
};